<template>
  <div class="city-group">
    <van-index-bar :index-list="indexList">
      <van-index-anchor index="热门" />
      <div class="list">
        <template v-for="(item, key) in groupData.hotCities" :key="key">
          <div class="city" @click="itemClick(item)">
            {{ item.cityName }}
          </div>
        </template>
      </div>

      <template v-for="(item, index) in groupData.cities" :key="index">
        <van-index-anchor :index="item.group" />
        <template v-for="(childItem, childIndex) in item.cities" :key="childIndex">
          <van-cell :title="childItem.cityName" @click="itemClick(childItem)"/>
        </template>
      </template>
    </van-index-bar>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import useCityStore from '@/stores/modules/city'
import { storeToRefs } from 'pinia'

import { useRouter } from 'vue-router'

const router = useRouter();

const props = defineProps({
  groupData: {
    type: Object,
    default: () => ({})
  }
})


const indexList = computed(() => {
  const result = props.groupData.cities.map((item) => item.group)
  result.unshift('#')
  return result;
})


const cityStore = useCityStore();
const itemClick = (city) => {
  cityStore.currentCity = city
  router.back();
}

</script>

<style lang="less" scoped>
.city-group {
  .list {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    padding: 10px;
    padding-right: 25px;

    .city {
      width: 70px;
      height: 28px;
      border-radius: 14px;
      font-size: 12px;
      color: #000;
      text-align: center;
      line-height: 28px;
      background-color: #fff4ec;
      margin: 6px 0;
    }
  }
}
</style>